import { CONTENT_TYPE } from '@/constants';
import service from './service';

const request = (option: AxiosConfig) => {
  const { url, method, params, data, headers, responseType } = option;
  return service.request({
    url,
    method,
    params,
    data,
    responseType,
    headers: {
      'Content-Type': CONTENT_TYPE,
      // TOOD: 需要添加token
      ...headers
    }
  });
};

export default {
  get: <T = any>(option: AxiosConfig) => {
    return request({ method: 'get', ...option }) as Promise<IResponse<T>>;
  },
  post: <T = any>(option: AxiosConfig) => {
    return request({ method: 'post', ...option }) as Promise<IResponse<T>>;
  },
  put: <T = any>(option: AxiosConfig) => {
    return request({ method: 'put', ...option }) as Promise<IResponse<T>>;
  },
  delete: <T = any>(option: AxiosConfig) => {
    return request({ method: 'delete', ...option }) as Promise<IResponse<T>>;
  },
  cancelRequest: (url: string | string[]) => {
    service.cancelRequest(url);
  },
  cancelAllRequest: () => {
    service.cancelAllRequest();
  }
};
